{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "feab660d-2231-4911-ab09-da9d7c6a5397",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Ch03-3 Sequence Manipulation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4d62f32a-abf2-4964-af3c-c9c9ad8fcd35",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Function Definition\n",
    "def parse_fasta(file_path):\n",
    "    \"\"\"\n",
    "    Parses a FASTA file and returns a dictionary with sequence headers as keys and sequences as values.\n",
    "    Parameters:\n",
    "    - file_path (str): Path to the FASTA file.\n",
    "    Returns:\n",
    "    - dict: A dictionary where keys are sequence headers and values are sequences.\n",
    "    \"\"\"\n",
    "    fasta_dict = {}\n",
    "    with open(file_path, 'r') as file:\n",
    "        header = None\n",
    "        sequence = []\n",
    "        for line in file:\n",
    "            line = line.strip()\n",
    "            if line.startswith(\">\"):  # Header line\n",
    "                if header:  # Save the previous sequence\n",
    "                    fasta_dict[header] = ''.join(sequence)\n",
    "                header = line[1:]  # Remove \">\"\n",
    "                sequence = []  # Reset sequence list\n",
    "            else:\n",
    "                sequence.append(line)\n",
    "        if header:  # Save the last sequence\n",
    "            fasta_dict[header] = ''.join(sequence)\n",
    "    return fasta_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "16c87949-80c7-409b-8fe4-f9db06031c42",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Header: seq1\n",
      "Sequence: ATCGTACGATCGGATCGTACGATC\n",
      "Header: seq2\n",
      "Sequence: CGTAGCTAGCTA\n"
     ]
    }
   ],
   "source": [
    "# Example usage:\n",
    "fasta_file = \"example.fasta\"\n",
    "fasta_records = parse_fasta(fasta_file)\n",
    "for header, seq in fasta_records.items():\n",
    "    print(f\"Header: {header}\")\n",
    "    print(f\"Sequence: {seq}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "347b2c76-3fe4-4a90-a31b-cb91f7abc157",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "695b0e93-5357-4d2d-925e-72c70dfafb07",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a41df267-cab9-4ff8-aee5-5efaa8d83982",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b238aba9-097d-4aa6-b424-9280c54a1e8d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ee8aae9-1366-4eea-8392-5b8e16a1d84a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
